﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using java.math;

namespace EpcTagLibrary.Pat
{
    public class PaddedPatternField : PatternField
    {
        private string m_PaddedHighRange;
        private string m_PaddedLowRange;
        private string m_PaddedExact;

        public PaddedPatternField(string patComponent, bool isGrouping, int maxBits)
            : base(patComponent, isGrouping, maxBits)
        {
            m_PaddedExact = m.Groups[1].Value;
            m_PaddedLowRange = m.Groups[2].Value;
            m_PaddedHighRange = m.Groups[3].Value;
        }

        public string PaddedExact
        {
            get
            {
                return m_PaddedExact;
            }
            set
            {
                m_PaddedExact = value;
            }
        }

        public string PaddedLowRange
        {
            get
            {
                return m_PaddedLowRange;
            }
            set
            {
                m_PaddedLowRange = value;
            }
        }

        public string PaddedHighRange
        {
            get
            {
                return m_PaddedHighRange;
            }
            set
            {
                m_PaddedHighRange = value;
            }
        }

        public override bool MatchField(EPC epc)
        {
            bool Result = false;

            if (IsWild)
            {
                Result = true;
            }
            else
            {
                BigInteger BigValue;
                string StringValue;
                try
                {
                    StringValue = (string)Property.GetValue(epc, new object[0]);
                    BigValue = new BigInteger(StringValue, 10);
                }
                catch (Exception e)
                {
                    throw new PatternMatchException("Matching Error.",e);
                }

                Result = IsRange?(BigValue.compareTo(LowRange)>=0) && (BigValue.compareTo(HighRange)<=0) && (StringValue.Length == PaddedHighRange.Length) && (StringValue.Length == PaddedLowRange.Length) : (BigValue.compareTo(Exact)==0) && (StringValue.Length == PaddedExact.Length);
            }

            return Result;
        }

        public override GroupField GroupField(EPC epc, GroupField parent)
        {
            GroupField Result = null;

            if (MatchField(epc))
            {
                if (IsGrouping)
                {
                    try
                    {
                        Result = parent.FindOrAddGroup(this, (string)Property.GetValue(epc, new object[0]));
                    }
                    catch (Exception e)
                    {
                        throw new PatternMatchException("Matching Error.", e);
                    }
                }
                else
                {
                    Result = parent.FindOrAddGroup(this, Component);
                }
            }

            return Result;
        }

        public override string ToString()
        {
            StringBuilder Result = new StringBuilder();

            Result.Append("PaddedPatternField(");
            Result.Append(Component);
            Result.Append("){");

            if (!IsRange && !IsWild)
            {
                Result.Append("Exact:");
                Result.Append(PaddedExact);
            }
            else if (IsRange)
            {
                Result.Append("Range :");
                Result.Append(PaddedLowRange);
                Result.Append("-");
                Result.Append(PaddedHighRange);
            }
            else
            {
                Result.Append("Any");
            }

            Result.Append("}");

            return Result.ToString();
        }

    }
}
